#include "test.h"
#include "gui.h"
#include "menu.h"
#include "stdlib.h"
#include "binary.h"


const uint8_t Pic0[] = {
  B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,
  B11110000,B00000000,B00000000,B00000000,
  B10001111,B00000000,B00000000,B00000000,
  B11000000,B11110000,B00000000,B00000000,
  B10100001,B00001111,B00000000,B00011100,
  B10010010,B00000100,B11110000,B00111110,
  B10001100,B00000011,B00111111,B11111111,
  B10001100,B00000011,B00111111,B11111111,
  B10010010,B00000100,B11110000,B00111110,
  B10100001,B00001111,B00000000,B00011100,
  B11000000,B11110000,B00000000,B00000000,
  B10001111,B00000000,B00000000,B00000000,
  B11110000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000
};

const uint8_t Pic1[] = {
  B00000111,B11100000,B00000000,B00000000,
  B00000100,B10110000,B00000000,B00000000,
  B00000100,B11011000,B00000000,B00000000,
  B00000100,B01001100,B00000000,B00000000,
  B00000100,B01100100,B00000000,B00000000,
  B00000100,B00111100,B00000000,B00000000,
  B00000100,B00001110,B00000000,B00000000,
  B00000100,B00000011,B00000000,B00000000,
  B00000100,B00000011,B00000000,B00000000,
  B00000100,B00000011,B10000000,B00000000,
  B00000100,B00000010,B10000000,B00000000,
  B00000100,B00000011,B11000000,B00000000,
  B00000100,B00000001,B11100000,B00000000,
  B00000100,B00000000,B11110000,B00000000,
  B00000100,B00000000,B01111100,B00000000,
  B00000100,B00000000,B00001110,B00000000,
  B00000100,B00000000,B00000011,B00000000,
  B00000100,B11111111,B11110001,B00000000,
  B00000100,B11000011,B10011001,B00000000,
  B00000100,B10000001,B00001001,B10000000,
  B00000100,B10000001,B00001000,B10000000,
  B00000100,B10000001,B00001000,B10000000,
  B00000100,B10000001,B00001000,B10000000,
  B00000100,B10000001,B00001000,B10000000,
  B00000100,B11000011,B10011001,B10000000,
  B00000100,B11111111,B11110001,B00000000,
  B00000100,B00000000,B00000001,B00000000,
  B00000100,B00000000,B00000001,B00000000,
  B00000100,B00000000,B00000001,B00000000,
  B00000100,B00000001,B11111110,B00000000,
  B00000100,B00000011,B00000110,B00000000,
  B00000100,B00000100,B00001100,B00000000,
  B00000100,B00000100,B00111000,B00000000,
  B00000100,B00000100,B11100000,B00000000,
  B00000100,B00000111,B10000000,B00000000,
  B00000111,B11111000,B00000000,B00000000
};
const uint8_t Mask1[] = {
  B00000111,B11100000,B00000000,B00000000,
  B00000111,B11110000,B00000000,B00000000,
  B00000111,B11111000,B00000000,B00000000,
  B00000111,B11111100,B00000000,B00000000,
  B00000111,B11111100,B00000000,B00000000,
  B00000111,B11111100,B00000000,B00000000,
  B00000111,B11111110,B00000000,B00000000,
  B00000111,B11111111,B00000000,B00000000,
  B00000111,B11111111,B00000000,B00000000,
  B00000111,B11111111,B10000000,B00000000,
  B00000111,B11111111,B10000000,B00000000,
  B00000111,B11111111,B11000000,B00000000,
  B00000111,B11111111,B11100000,B00000000,
  B00000111,B11111111,B11110000,B00000000,
  B00000111,B11111111,B11111100,B00000000,
  B00000111,B11111111,B11111110,B00000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111111,B10000000,
  B00000111,B11111111,B11111111,B10000000,
  B00000111,B11111111,B11111111,B10000000,
  B00000111,B11111111,B11111111,B10000000,
  B00000111,B11111111,B11111111,B10000000,
  B00000111,B11111111,B11111111,B10000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111110,B00000000,
  B00000111,B11111111,B11111110,B00000000,
  B00000111,B11111111,B11111100,B00000000,
  B00000111,B11111111,B11111000,B00000000,
  B00000111,B11111111,B11100000,B00000000,
  B00000111,B11111111,B10000000,B00000000,
  B00000111,B11111000,B00000000,B00000000
};
const uint8_t Pic2[] = {
  B00011000,
  B00110100,
  B01001010,
  B10111011,
  B11011101,
  B01010010,
  B00101100,
  B00011000
};

const uint8_t Pic3[] = {
  B00000011,
  B00000011,
  B00000011,
  B00000011,
  B00000011,
  B00000111,
  B00001011,
  B00011111,
  B00101011,
  B00111111,
  B01010111,
  B01111111,
  B10101011,
  B11111111,
  B10101011,
  B01111111
};

const uint8_t Pic4[] = {
  B00000010,B00001100,
  B00000101,B00010010,
  B00001001,B00100010,
  B00001010,B11000100,
  B00001010,B00010100,
  B00010000,B00100100,
  B00100001,B00001000,
  B01001011,B10001000,
  B10010001,B00000100,
  B10000000,B00010100,
  B01111001,B01101010,
  B00000101,B01010010,
  B00000100,B01001010,
  B00000010,B10000100,
  B00000001,B00000000
};

const uint8_t Mask4[] = {
  B00000010,B00001100,
  B00000111,B00011110,
  B00001111,B00111110,
  B00001111,B11111100,
  B00001111,B11111100,
  B00011111,B11111100,
  B00111111,B11111000,
  B01111111,B11111000,
  B11111111,B11111100,
  B11111111,B11111100,
  B01111111,B11111110,
  B00000111,B11011110,
  B00000111,B11001110,
  B00000011,B10000100,
  B00000001,B00000000
};

struct FLOWER{
    int x;
    int y;
    float arg;
    uint8_t mode;
    uint8_t state;
  };

void TEST_Flower_Init(struct FLOWER* flower)
{
  flower->x = rand()%128;
  flower->y = -15;
  flower->arg = rand()%360;
  flower->mode = rand()%2;
  flower->state = 0x01;
}

void TEST_Flower_Move(struct FLOWER* flower)
{
  if(flower->state != 0x01) return;
  //flower->x += (rand()%2)?(1):(-1);
  flower->y += 1;
  flower->arg += 10;
  if(flower->x < -15 || flower->x > 145 || flower->y > 80)  flower->state = 0x00;
}

void TEST_Flower_Show(struct FLOWER* flower)
{
  if(flower->state != 0x01) return;
  
  POINT point;
  int x = flower->x, y = flower->y;
  
  GUI_SetSpanAng(flower->arg);
  point = GUI_SetSpanCenter(x,y,x+8,y+8);
  x = point.x;
  y = point.y;
  if(flower->mode == 0x00)  GUI_ShowBMP(x,y,x+15,y+15,Pic4,Transparent|SpanSingle);
  else                      GUI_ShowPNG(x,y,x+15,y+15,Mask4,Pic4,SpanSingle);
}

void TEST_ShowFlower(void)
{
  struct FLOWER flowers[10];
  uint8_t i = 0;
  
  while(1)
  {
    for(i = 0; i < 10; i++)
    {
      if(flowers[i].state != 0x01 && (rand()%8 == 0))
      {
        TEST_Flower_Init(&flowers[i]);
        break;
      }
    }
    for(i = 0; i < 10; i++)
    {
      if(flowers[i].state == 0x01)
      {
        TEST_Flower_Move(&flowers[i]);
        TEST_Flower_Show(&flowers[i]);
      }
    }
    
    GUI_ShowMap();
    GUI_ClearMap(0x00);
    HAL_Delay(20);
  }
  
}

void TEST_ShowSence(void)
{
  int i = 0;
  int x = 0, y = 0;
  uint8_t type = 0;

  for(x = 100, i = 0; x > 0; x--)
  {
    GUI_ClearMap(0x00);
    
    TEST_ShowWindmill(90-x,5,1080-3*i);
    TEST_ShowWindmill(20-x,15,720-2*i);
    
    i++;
    if(i == 360)	i = 0;
    
    GUI_ShowMap();
    HAL_Delay(20);
  }

  y = 32;
  x = -36;
  while(True)
  {
    GUI_ClearMap(0x00);
    
    TEST_ShowWindmill(90,5,1080-3*i);
    TEST_ShowWindmill(20,15,720-2*i);
    TEST_ShowCar(2*x,y-18,i,type);
    TEST_ShowCar(x,y,i,type);
    i++;
    if(i == 360)	i = 0;
    if(i%2 == 1)	x++;
    if(x > 128)
    {
      x = -36;
      type = !type;
    }
    
    GUI_ShowMap();
    HAL_Delay(20);
  }
}

void TEST_ShowCar(int x, int y, float arg, uint8_t type)
{
  POINT point;
  if(type)	GUI_ShowBMP(x,y,x+36,y+32,Pic1,Transparent|L2R|U2D);
  else			GUI_ShowPNG(x,y,x+36,y+32,Mask1,Pic1,L2R|U2D);
  GUI_SetSpanAng(arg);
  point = GUI_SetSpanCenter(x+4,y+23,x+8,y+27);
  GUI_ShowBMP(point.x,point.y,point.x+8,point.y+8,Pic2,Transparent|SpanSingle);
  point = GUI_SetSpanCenter(x+27,y+23,x+31,y+27);
  GUI_ShowBMP(point.x,point.y,point.x+8,point.y+8,Pic2,Transparent|SpanSingle);
}

void TEST_ShowWindmill(int x, int y, float arg)
{
  uint8_t n = 0;
  GUI_ShowBMP(x,y,x+16,y+32,Pic0,Transparent|U2D);
  for(n = 0; n < 4; n++)
  {
    GUI_SetSpanAng(arg+n*90);
    GUI_ShowBMP(x+8,y+3,x+8+16,y+3+8,Pic3,Transparent|SpanSingle|U2D);
  }
}

void TEST_ShowLine(void)
{
  uint16_t i = 0;
  GUI_ClearMap(0x00);
  for(i = 0; i <= 90; i += 5)
  {
    GUI_DrawLine(0,0,i,128,1);
    GUI_ShowMap();
    HAL_Delay(10);
  }
  for(i = 0; i <= 90; i += 5)
  {
    GUI_DrawLine(128,0,i+90,128,1);
    GUI_ShowMap();
    HAL_Delay(10);
  }
  for(i = 0; i <= 90; i += 5)
  {
    GUI_DrawLine(128,64,i+180,128,1);
    GUI_ShowMap();
    HAL_Delay(10);
  }
  for(i = 0; i <= 90; i += 5)
  {
    GUI_DrawLine(0,64,i+270,128,1);
    GUI_ShowMap();
    HAL_Delay(10);
  }
  HAL_Delay(500);
  HAL_Delay(500);
}

void TEST_ShowMenu(void)
{
  uint8_t num = 4;
  int index = 0;
  uint8_t n = 0;
  float p = 0, i = 0, d = 0, q = 0, r = 0, z = 0;
  struct MENU menu1,menu2,menu3,menu4,menu5,menu6;
  POINT point = {0, 0};

  MENU_InitMenu(&menu1,"menu1",'P', &p, point);
  MENU_InitMenu(&menu2,"menu2",'I', &i, point);
  MENU_InitMenu(&menu3,"menu3",'D', &d, point);
  MENU_InitMenu(&menu4,"menu4",'Q', &q, point);
  MENU_InitMenu(&menu5,"menu5",'R', &r, point);
  MENU_InitMenu(&menu6,"menu6",'Z', &z, point);

  while(True)
  {
    GUI_ClearMap(0x00);
    for(n = 1; n <= 3; n++)
    {
      MENU_SetValue(n,(float)index/(n+1)+(float)n/(index+1));
    }
    q = index/3;
    r = index/4.0;
    z = index;
    MENU_ShowMenu(SIZE16);
    if(index<32)
      MENU_MoveMenu(0,-1);
    else
      MENU_MoveMenu(0,1);
    index++;
    if(index >= 64)
    {
      if(num == 3)	break;
      MENU_DistoryMenu(num);
      num = num % 6 + 1;
      if(num == 1)	MENU_InitMenu(&menu5,"menu5",'I', &i, point);
      index = 0;
    }
    
    GUI_ShowMap();
    HAL_Delay(50);
  }
  MENU_DistoryAllMenu();
}

void TEST_ShowChinese(void)
{
  POINT point;
  uint8_t nums[6] = {0,1,2,3,4,5};
  int i = 0;
  uint8_t state = 0;
  uint8_t mode = Normal;

  mode = Transparent|SpanSingle;
  while(True)
  {
    if((mode&Transparent)&&(mode&Reverse))
    {
      GUI_ClearMap(0xff);
      GUI_ShowFloat(0,0,i-180,2,SIZE12,Reverse|Transparent);
    }
    else
    {
      GUI_ClearMap(0x00);
      GUI_ShowFloat(0,0,i-180,2,SIZE12,Normal);
    }
    
    GUI_SetSpanAng(i);
    if(mode&SpanSingle)
      point = GUI_SetSpanCenter(16,24,64,32);
    else
    {
      point.x = 16;
      point.y = 24;
    }
    
    GUI_ShowChineseStr(point.x,point.y,nums,6,mode);
    
    i+=2;
    if(i == 360)
    {
      i = 0;
      state++;
      if(state == 0)	mode = Transparent|SpanSingle;
      else if(state == 1)	mode = Transparent|Span;
      else if(state == 2)	mode = Transparent|SpanSingle|U2D|L2R|Reverse;
      else	break;
    }
    
    GUI_ShowMap();
    HAL_Delay(50);
  }
}

void TEST_Circle(void)
{
  uint8_t i = 0;
  GUI_ClearMap(0x00);
  for(i = 1; i < 30; i++)
  {
    GUI_circle(63, 31, i);
    GUI_ShowMap();
    HAL_Delay(50);
  }

  GUI_ClearMap(0x00);
  for(i = 1; i < 30; i++)
  {
    GUI_circleunfilled(63, 31, i, 2);
    GUI_ShowMap();
    HAL_Delay(50);
  }
  for(i = 1; i < 30; i++)
  {
    GUI_ClearMap(0x00);
    GUI_circleunfilled(63, 31, i, 2);
    GUI_ShowMap();
    HAL_Delay(50);
  }
}








